package cn.edu.nju.software.cdiy.interceptor;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import cn.edu.nju.software.cdiy.util.BusinessConstant;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

@SuppressWarnings("serial")
public class SupplierLoginInterceptor extends AbstractInterceptor {

	private String url;

	public void setUrl(String url) {
		this.url = url;
	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		Map<String, Object> session = invocation.getInvocationContext().getSession();
		HttpServletRequest request = ServletActionContext.getRequest();
		
		if (!session.containsKey(BusinessConstant.S_SUPPLIER)) {
			if (url == null) {
				String queryString = request.getQueryString();
				if (queryString == null) {
					queryString = "";
				} else {
					queryString = "?" + queryString;
				}
				session.put(BusinessConstant.S_SUPPLIER_URL, request.getServletPath()+queryString);
			} else {
				session.put(BusinessConstant.S_SUPPLIER_URL, url);
			}
			return Action.LOGIN;
		}
		
		return invocation.invoke();
	}

}
